/*
 * PageBuilder.cpp
 *
 *  Created on: 27/06/2014
 *      Author: FILIPE
 */

#include "PageBuilder.h"

void PageBuilder::build_home_page(User user) {
	File entrada("./html/template_home.html", "rb");
	auto vec = entrada.read_file_contents();
	std::string page_array(vec.begin(), vec.end());
	char num[3];

	page_array.replace(page_array.find("#nomeusr"), 8, user.get_name());

	Globals::peers.lock();
	int tam = Globals::peers.get().size();
	Globals::peers.unlock();
	std::string aviso;
	if(tam == 2){
		aviso = "Pode dar ruim a qualquer momento";
	}else if(tam < 2){
		aviso = "Vai dar ruim concerteza";
	}else{
		aviso = "Por enquanto est&aacute; de boa";
	}
	std::string s_tam;
	std::string tabela("<table border=\"1\">\n<tr><th><font color=\"white\">Nome</font></th><th><font color=\"white\">Local</font></th><th><font color=\"white\">Local_C&oacute;pia</font></th><th><font color=\"white\">Size(bytes)</font></th></tr>\n");
	Globals::file_table.lock();
	char tamanho[20];
	unsigned int total_size = 0;

	for(auto it = Globals::file_table.get()->get_file_table().begin(); it != Globals::file_table.get()->get_file_table().end(); it++){
		tabela += "<tr>\n<td><font color=\"white\">" + it->second->get_name() + "</font></td>\n";
		tabela += "<td><font color=\"white\">" + convert_ip_to_string(it->second->get_location()) + "</font></td>\n";
		tabela += "<td><font color=\"white\">" + convert_ip_to_string(it->second->get_copy_location()) + "</font></td>\n";
		total_size += it->second->get_size();
		sprintf(tamanho,"%d",it->second->get_size());
		s_tam = tamanho;
		tabela += "<td><font color=\"white\">" + s_tam + "</font></td>\n</tr>\n";
	}

	Globals::file_table.unlock();
	tabela += "</table>\n";

	sprintf(num, "%d", tam);
	std::string ctam(num);
	page_array.replace(page_array.find("#pares"), 6, ctam);

	page_array.replace(page_array.find("#avisos"),7,aviso);

	sprintf(tamanho,"%d",2*total_size);
	s_tam = tamanho;

	page_array.replace(page_array.find("#sistam"),7,s_tam);

	page_array.replace(page_array.find("#tabela"),7,tabela);

	File saida("./html/home.html", "wb");

	std::vector<char> escrita(page_array.begin(), page_array.end());

	saida.binary_write(escrita);
}

void PageBuilder::build_dir_page(std::string directory) {
	File entrada("./html/template_dir.html", "rb");
	auto vec = entrada.read_file_contents();
	std::string page_array(vec.begin(), vec.end());
	std::string path;

	auto found = directory.find_last_of("/");
	path = directory.substr(0, found);
	found = path.find_last_of("/");
	path = path.substr(0, found + 1);

	page_array.replace(page_array.find("#pasta"), 6, directory);

	page_array.replace(page_array.find("#parent"), 7, path);

	Globals::file_table.lock();

	auto element = Globals::file_table.get()->get_element_from_path(directory);

	auto directory_html = ((Directory*) element)->html_children_encoding();

	Globals::file_table.unlock();

	page_array.replace(page_array.find("#elementos"), 10, directory_html);

	File saida("./html/directory.html", "wb");
	std::vector<char> escrita(page_array.begin(), page_array.end());
	saida.binary_write(escrita);
}

std::string PageBuilder::convert_ip_to_string(unsigned int ip) {
	unsigned int bit24 = 0xFF000000;
	unsigned int bit16 = 0x00FF0000;
	unsigned int bit8 = 0x0000FF00;
	unsigned int bit0 = 0x000000FF;
	char ipAddr[20];
	sprintf(ipAddr, "%u.%u.%u.%u", (ip & bit0), (ip & bit8) >> 8,
			(ip & bit16) >> 16, (ip & bit24) >> 24);
	return std::string(ipAddr);
}
